package bc

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/bc"
	bcReq "github.com/flipped-aurora/gin-vue-admin/server/model/bc/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BcTempApi struct {
}

var bcTempService = service.ServiceGroupApp.BcServiceGroup.BcTempService

// CreateBcTemp 创建bcTemp表
// @Tags BcTemp
// @Summary 创建bcTemp表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcTemp true "创建bcTemp表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /bcTemp/createBcTemp [post]
func (bcTempApi *BcTempApi) CreateBcTemp(c *gin.Context) {
	var bcTemp bc.BcTemp
	err := c.ShouldBindJSON(&bcTemp)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcTempService.CreateBcTemp(&bcTemp); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteBcTemp 删除bcTemp表
// @Tags BcTemp
// @Summary 删除bcTemp表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcTemp true "删除bcTemp表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /bcTemp/deleteBcTemp [delete]
func (bcTempApi *BcTempApi) DeleteBcTemp(c *gin.Context) {
	var bcTemp bc.BcTemp
	err := c.ShouldBindJSON(&bcTemp)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcTempService.DeleteBcTemp(bcTemp); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteBcTempByIds 批量删除bcTemp表
// @Tags BcTemp
// @Summary 批量删除bcTemp表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除bcTemp表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /bcTemp/deleteBcTempByIds [delete]
func (bcTempApi *BcTempApi) DeleteBcTempByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcTempService.DeleteBcTempByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateBcTemp 更新bcTemp表
// @Tags BcTemp
// @Summary 更新bcTemp表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcTemp true "更新bcTemp表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /bcTemp/updateBcTemp [put]
func (bcTempApi *BcTempApi) UpdateBcTemp(c *gin.Context) {
	var bcTemp bc.BcTemp
	err := c.ShouldBindJSON(&bcTemp)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcTempService.UpdateBcTemp(bcTemp); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindBcTemp 用id查询bcTemp表
// @Tags BcTemp
// @Summary 用id查询bcTemp表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query bc.BcTemp true "用id查询bcTemp表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /bcTemp/findBcTemp [get]
func (bcTempApi *BcTempApi) FindBcTemp(c *gin.Context) {
	var bcTemp bc.BcTemp
	err := c.ShouldBindQuery(&bcTemp)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if rebcTemp, err := bcTempService.GetBcTemp(bcTemp.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"rebcTemp": rebcTemp}, c)
	}
}

// GetBcTempList 分页获取bcTemp表列表
// @Tags BcTemp
// @Summary 分页获取bcTemp表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query bcReq.BcTempSearch true "分页获取bcTemp表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /bcTemp/getBcTempList [get]
func (bcTempApi *BcTempApi) GetBcTempList(c *gin.Context) {
	var pageInfo bcReq.BcTempSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := bcTempService.GetBcTempInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (bcTempApi *BcTempApi) DeleteBcTempByTime() {
	fmt.Println("开始批量删除取号记录....")
	var IDS request.IdsReq
	list, err := bcTempService.GetBcTempExpireList()
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		return
	}
	for b := 0; b < len(list); b++ {
		IDS.Ids = append(IDS.Ids, int(list[b].ID))
	}
	if err := bcTempService.DeleteBcTempByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除取号记录失败!", zap.Error(err))
	} else {
		fmt.Println("批量删除取号记录成功")
	}
}
